// @flow
import { warn, log } from "./utils"
import { mountComponent } from "./lifecycle"
import { query, getOuterHTML } from "./utils/web.js"

let uid = 100;

// fixme
let inBrowser = true;
class Ken {
	_uid: number;
	$el: ?Element;
	$options: Object;
	constructor(options: Object){
		// 判断如果是开发环境
		// 不允许直接执行 Ken()这个函数
		// 必须采取 new Ken()
		if( process.env.NODE_ENV !== "production" && !(this instanceof Ken) ) {
			warn("Ken is a constructor and should be called with the `new` keyword'")
		}
		// 将传进来的option挂载到$option
		this.$options = options || {};
		this._uid = uid++;
		// 打印this对象
		log("main start", this);
		// 打印日志
		log('main start', options);
		// 初始化
		this._init(this.$options);
	}
	// get _uid(): number {
	// 	return uid++;
	// }

	// get $options(): Object{
	// 	return {};
	// }

	// set $options(options: Object) {
	// 	log("main start", options);
	// }

	$mount(el: Element | string, hydrating?: boolean){
		// 获取元素
		let element = query(el);
		if(!this.$options.template){
			// 判断是否存在template
			 this.$options.template = getOuterHTML(element);
		}

		if(!this.$options.template && !element){
			warn("options.el && options.template all null");
			return ;
		}
		this.$el = element;
		log("$mount", this);
		return mountComponent(this, element, hydrating);
	}

	_init(options?: Object){
		let el: string | Element = options.el;

		if(el && inBrowser) {
			this.$mount(el);
		}
	}



}
export default Ken;